package com.maikang.exammall.repository;

import com.maikang.exammall.entity.OrderItem;
import com.maikang.exammall.entity.dto.order.IdOnlyDTO;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository
public interface OrderItemRepository extends BaseRepository<OrderItem, Long>, JpaSpecificationExecutor<OrderItem> {



    @Query("UPDATE OrderItem item SET item.status = :status WHERE item.id IN (:ids)")
    @Modifying
    void updateStatusByIds(@Param("status") Integer status, @Param("ids") List<Long> ids);

    @Modifying
    @Query("UPDATE OrderItem item SET item.status = :toStatus WHERE item.status = :fromStatus AND item.orderId iN (:orderIds)")
    void updateStatusByOrderIdAndStatus(@Param("fromStatus") Integer fromStatus, @Param("toStatus") Integer toStatus, @Param("orderIds")List<Long> orderIds);

    @Modifying
    @Query(value = "UPDATE mko_order_item SET status = :toStatus WHERE status = :fromStatus AND id iN (:ids)", nativeQuery = true)
    void updateStatusByIdsAndStatus(@Param("fromStatus") Integer fromStatus, @Param("toStatus") Integer toStatus, @Param("ids")List<Long> ids);

    // 2 means paid 只查询过去7天
    @Query(value = "SELECT id, order_id AS orderId " +
            "FROM mko_order_item " +
            "WHERE status = 2 " +
            "AND create_time > DATE_SUB(curdate(), INTERVAL 7 DAY) AND is_deleted = 0 " +
            "AND examine_date <= DATE_SUB(curdate(), INTERVAL 1 DAY) ", nativeQuery = true)
    List<IdOnlyDTO> findExaminedOrderItem();
}
